package com.zzl.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zzl.entity.PageBean;
import com.zzl.entity.Student;
import com.zzl.services.StudentServices;
import com.zzl.services.StudentServices;
import com.zzl.services.impl.StudentServicrImpl;
import com.zzl.util.DbUtil;
import com.zzl.util.JsonUtil;
import com.zzl.util.ResponseTypeUtil;
import com.zzl.util.StringEmptyUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class StudentListServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    StudentServices studentService = new StudentServicrImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                this.doPost(request, response);
            }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                String stuNo = request.getParameter("stuNo");
                String stuName = request.getParameter("stuName");
                String sex = request.getParameter("sex");
                String bbirthday = request.getParameter("bbirthday");
                String ebirthday = request.getParameter("ebirthday");
                String gradeId = request.getParameter("gradeId");

                //创建学生对象
                Student student = new Student();
                if (stuNo != null) {
                    student.setStuNo(stuNo);
                    student.setStuName(stuName);
                    student.setSex(sex);
                    if (StringEmptyUtil.isNotEmpty(gradeId)) {
                        student.setGradeId(Integer.parseInt(gradeId));
                    }
                }

                String page = request.getParameter("page");
                String rows = request.getParameter("rows");

                PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
                Connection con = null;
                try {
                    con = dbUtil.getCon();
                    JSONObject result = new JSONObject();
                    JSONArray jsonArray = JsonUtil.formatRsToJsonArray(studentService.studentList(con, pageBean, student, bbirthday, ebirthday));
                    int total = studentService.studentCount(con, student, bbirthday, ebirthday);
                    result.put("rows", jsonArray);
                    result.put("total", total);

                    ResponseTypeUtil.write(response, result);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        dbUtil.closeCon(con);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                request.getRequestDispatcher("studentList.jsp").forward(request, response);
            }
}
